package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-05-27
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     * */
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            //获取文件输入流
            InputStream in=file.getInputStream();
            //调用EasyExcel读取excel文件内容
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 以树形结构获取所有Subject
     * */
    @Override
    public List<OneSubject> getAllSubjectTree() {

        //1、查询所有一级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");//eq为等于
        //baseMapper是父类属性，用于调用mapper层
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //2、查询所有二级分类（除一级分类以为的所有分类） parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id","0");//ne为不等于
        //baseMapper是父类属性，用于调用mapper层
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建list集合，存储最终返回数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //3、封装一级分类
        for (EduSubject oneEdu : oneSubjectList){
            OneSubject oneSubject=new OneSubject();

//            oneSubject.setId(oneEdu.getId());
//            oneSubject.setTitle(oneEdu.getTitle());
            //工具类写法
            BeanUtils.copyProperties(oneEdu,oneSubject);

            //4、封装二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (EduSubject twoEdu : twoSubjectList){
                if (twoEdu.getParentId().equals(oneEdu.getId())){
                    TwoSubject twoSubject=new TwoSubject();
                    BeanUtils.copyProperties(twoEdu,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            //把筛选好的二级分类放入对应的一级分类
            oneSubject.setChildren(twoFinalSubjectList);

            //将处理好的一级分类放入返回列表
            finalSubjectList.add(oneSubject);
        }

        return finalSubjectList;
    }
}
